package com.hzsd.zengbanyi.biaodan_text.Actrivity;


import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hzsd.zengbanyi.biaodan_text.BaseActivity;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.WorkDAO;

/**
 * 日志（定位误差）页面
 * 无效页面(已废除)
 * @author Administrator
 *
 */
public class GpsErrorLogActivity extends BaseActivity implements OnClickListener{
	private RelativeLayout rl_return;
	private TextView tv_save;
	private EditText et_gpslog;
	
	private String sampleId;
	private String memo;//日志内容
	private boolean force = false;//是否强制写日志（true:是）

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_gps_error_log);
		initView();
		initBaseData();
	}

	private void initBaseData() {
		Bundle bundle = getIntent().getExtras();
		sampleId = bundle.getString("sampleId");
		force = bundle.getBoolean("force");
		WorkDAO workDAO = new WorkDAO(GpsErrorLogActivity.this);
		memo = workDAO.getSmplePointLog(sampleId);
		et_gpslog.setText(memo);
		if (force) {
			rl_return.setVisibility(View.GONE);
		}
	}

	private void initView() {
		rl_return = (RelativeLayout) findViewById(R.id.rl_return);
		tv_save = (TextView) findViewById(R.id.tv_save);
		et_gpslog = (EditText) findViewById(R.id.et_gpslog);
		
		rl_return.setOnClickListener(this);
		tv_save.setOnClickListener(this);
		
		et_gpslog.addTextChangedListener(new TextWatcher() {
			
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				if ("".equals(et_gpslog.getText().toString().trim())) {
					tv_save.setVisibility(View.GONE);
				}else {
					tv_save.setVisibility(View.VISIBLE);
				}
				
			}
		});
		
	}
	
	@Override
	public void onBackPressed() {
		if (force) {
			if ("".equals(et_gpslog.getText().toString().trim())) {
				Builder builder = new Builder(GpsErrorLogActivity.this);
				builder.setTitle("提示");
				builder.setMessage("您的位置超出预定误差范围，请填写日志！");
				builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						
						
					}
				});
				builder.create().show();
			}else {
				Builder builder = new Builder(GpsErrorLogActivity.this);
				builder.setTitle("提示");
				builder.setMessage("是否保存日志？");
				builder.setPositiveButton("保存日志", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						Intent data = new Intent();
						data.putExtra("gpslog", et_gpslog.getText().toString());
						setResult(RESULT_OK, data);
						finish();
						
					}
				});
				builder.setNegativeButton("再写写", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				builder.create().show();
			}
			
		}else {
			finish();
		}
		
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rl_return:
			finish();
			break;
		case R.id.tv_save:
			Intent data = new Intent();
			data.putExtra("gpslog", et_gpslog.getText().toString());
			setResult(RESULT_OK, data);
			finish();
			break;

		default:
			break;
		}
		
	}

}
